﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Brain.Rendering;

namespace Brain
{
    public class BezierCurveEditor2D
    {
        public BezierCurve2D EdittingCurve { get; set; }

        public BezierCurveEditor2D()
        {
        }

        public void Update()
        {



        }

        private bool manipulating;
        private bool control;
        private BezierPoint2D maniPoint;

        public void Draw()
        {
            if (EdittingCurve == null)
            {
                return;
            }

            Vector2 mouse = Engine.Instance.InputManager.GetMousePosition();
            if (Engine.Instance.CameraManager != null &&
                Engine.Instance.CameraManager.Camera2D != null)
            {
                Matrix m = Matrix.Invert(Engine.Instance.CameraManager.Camera2D.World);
                mouse = Vector2.Transform(mouse, m);
            }

            Rectangle mouseRect = new Rectangle((int)mouse.X, (int)mouse.Y, 1, 1);

            IDrawer2D drawer = Engine.Instance.RenderManager2D.Drawer;

            if (manipulating)
            {
                if (Engine.Instance.InputManager.IsMouseLeftUp())
                {
                    manipulating = false;
                }

                if (control)
                {
                    maniPoint.Control = mouse;
                }
                else
                {
                    maniPoint.Point = mouse;
                }
            }

            drawer.Begin();
            int size = 16;
            int size2 = size / 2;
            for (int i = 0; i < EdittingCurve.Points.Count; i++)
            {
                BezierPoint2D point = EdittingCurve.Points[i];

                Rectangle pointRect = RectangleUtil.FromVector2(point.Point - new Vector2(size2, size2), size, size);
                Rectangle controlRect = RectangleUtil.FromVector2(point.Control - new Vector2(size2, size2), size, size);

                if (!manipulating)
                {
                    if (Engine.Instance.InputManager.IsMouseLeftDown())
                    {
                        if (pointRect.Contains(mouseRect))
                        {
                            manipulating = true;
                            control = false;
                            maniPoint = point;
                        }
                        else if (controlRect.Contains(mouseRect))
                        {
                            manipulating = true;
                            control = true;
                            maniPoint = point;
                        }
                    }
                }

                drawer.DrawRectangle(pointRect,
                    Color.Red);
                drawer.DrawRectangle(controlRect,
                    Color.Yellow);
                drawer.DrawLine(point.Control, point.Point, Color.White, 2);
                
            }
            drawer.End();
        }
    }
}
